package com.ruoyi.wb.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wb.domain.WbEntAuth;
import com.ruoyi.wb.domain.WbExit;
import com.ruoyi.wb.domain.WbMaintainCompany;
import com.ruoyi.wb.enmus.RoleNameEnums;
import com.ruoyi.wb.mapper.WbExitMapper;
import com.ruoyi.wb.service.IWbEntAuthService;
import com.ruoyi.wb.service.IWbExitService;
import com.ruoyi.wb.service.IWbMaintainCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 疏散通道Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-14
 */
@Service
public class WbExitServiceImpl implements IWbExitService 
{
    @Autowired
    private WbExitMapper wbExitMapper;

    @Autowired
    private IWbEntAuthService iWbEntAuthService;

    @Autowired
    private IWbMaintainCompanyService iWbMaintainCompanyService;

    /**
     * 查询疏散通道
     * 
     * @param id 疏散通道主键
     * @return 疏散通道
     */
    @Override
    public WbExit selectWbExitById(Long id)
    {
        return wbExitMapper.selectWbExitById(id);
    }

    /**
     * 查询疏散通道列表
     * 
     * @param wbExit 疏散通道
     * @return 疏散通道
     */
    @Override
    public List<WbExit> selectWbExitList(WbExit wbExit)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = sysUser.getRoles();
        List<WbExit> wbExits = new ArrayList<>();

        for (SysRole role:roles) {
            if (role.getRoleName().equals(RoleNameEnums.ADMIN.getDescribe())||
                role.getRoleName().equals(RoleNameEnums.FIRE_UNIT_MANAGER.getDescribe())||
                role.getRoleName().equals(RoleNameEnums.FIREFIGHTER.getDescribe())
            ){//超级管理员 消防单位
                startPage();
                wbExits=wbExitMapper.selectWbExitList(wbExit);
            } else if (role.getRoleName().equals(RoleNameEnums.MAINTAIN_ADMIN.getDescribe())){//维保公司管理员
                List<WbMaintainCompany> wbMaintainCompanies = iWbMaintainCompanyService.selectWbMaintainCompanyByUserId(sysUser.getUserId());
                if (!CollectionUtils.isEmpty(wbMaintainCompanies)){
                    wbExit.setMaintainCompanyId(wbMaintainCompanies.get(0).getId());
                    startPage();
                    wbExits=wbExitMapper.selectWbExitList(wbExit);
                }
            }else if (role.getRoleName().equals(RoleNameEnums.DOMESTIC_CONSUMER.getDescribe())){//普通用户
                List<WbEntAuth> wbEntAuthList = iWbEntAuthService.selectWbEntAuthListByUserId(sysUser.getUserId());
                if (!CollectionUtils.isEmpty(wbEntAuthList)){
                    wbExit.setBid(wbEntAuthList.get(0).getId());
                    startPage();
                    wbExits=wbExitMapper.selectWbExitList(wbExit);
                }
            }
        }
        return wbExits;
    }
    /**
     * 新增疏散通道
     * 
     * @param wbExit 疏散通道
     * @return 结果
     */
    @Override
    public int insertWbExit(WbExit wbExit)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        List<WbMaintainCompany> wbMaintainCompanies = iWbMaintainCompanyService.selectWbMaintainCompanyByUserId(sysUser.getUserId());
        if (!CollectionUtils.isEmpty(wbMaintainCompanies)){
            wbExit.setMaintainCompanyId(wbMaintainCompanies.get(0).getId());
        }
        wbExit.setCreateTime(DateUtils.getNowDate());
        wbExit.setCreateBy(sysUser.getNickName());
        wbExit.setUpdateBy(sysUser.getNickName());
        wbExit.setUpdateTime(DateUtils.getNowDate());
        return wbExitMapper.insertWbExit(wbExit);
    }

    /**
     * 修改疏散通道
     * 
     * @param wbExit 疏散通道
     * @return 结果
     */
    @Override
    public int updateWbExit(WbExit wbExit)
    {
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        wbExit.setUpdateTime(DateUtils.getNowDate());
        wbExit.setUpdateBy(loginUser.getNickName());
        return wbExitMapper.updateWbExit(wbExit);
    }

    /**
     * 批量删除疏散通道
     * 
     * @param ids 需要删除的疏散通道主键
     * @return 结果
     */
    @Override
    public int deleteWbExitByIds(Long[] ids)
    {
        return wbExitMapper.deleteWbExitByIds(ids);
    }

    /**
     * 删除疏散通道信息
     * 
     * @param id 疏散通道主键
     * @return 结果
     */
    @Override
    public int deleteWbExitById(Long id)
    {
        return wbExitMapper.deleteWbExitById(id);
    }
}
